<template>
  <a-modal
    :title="title"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则名称">
          <a-input placeholder="请输入规则名称" v-decorator="['ruleName', {rules: [{required: true, message: '请输入规则名称!'}]}]"/>
        </a-form-item>
        <a-form-item
          v-show="showRuleColumn"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则字段">
          <a-input placeholder="请输入规则字段" v-decorator="['ruleColumn']"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="条件规则">
          <dict-select-tag @change="handleChangeRuleCondition" v-decorator="['ruleConditions', {rules: [{required: true, message: '请选择条件!'}]}]" placeholder="请输入条件规则" :triggerChange="true" dictCode="rule_conditions"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则值">
          <a-input placeholder="请输入规则值" v-decorator="['ruleValue', {rules: [{required: true, message: '请输入规则值!'}]}]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="状态">
          <a-radio-group buttonStyle="solid" v-decorator="['status',{initialValue:'1'}]">
            <a-radio-button value="1">有效</a-radio-button>
            <a-radio-button value="0">无效</a-radio-button>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import DictSelectTag from '@/components/dict/DictSelectTag'
import { apiAddPermissionRule, apiEditPermissionRule } from '@/api/system'
import pick from 'lodash.pick'

export default {
  name: 'PermissionDataRuleModal',
  components: {
    DictSelectTag
  },
  data () {
    return {
      visible: false,
      title: '',
      confirmLoading: false,
      form: this.$form.createForm(this),
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      showRuleColumn: true,
      model: {},
      permissionId: ''
    }
  },
  methods: {
    show (title, record) {
      this.title = title
      this.visible = true
      this.form.resetFields()
      if (record.permissionId) {
        this.model.permissionId = record.permissionId
      }
      if (record.id) {
        this.model = Object.assign({}, record)
        this.setFormValue()
      }
    },
    setFormValue () {
      if (this.model.ruleConditions && this.model.ruleConditions === 'USE_SQL_RULES') {
        this.showRuleColumn = false
      } else {
        this.showRuleColumn = true
      }
      this.$nextTick(() => {
        setTimeout(() => {
          this.form.setFieldsValue(pick(this.model, 'status', 'ruleName', 'ruleColumn', 'ruleConditions', 'ruleValue'))
        })
      })
    },
    close () {
      this.visible = false
    },
    handleOk () {
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          this.confirmLoading = true
          const formData = Object.assign(this.model, values)
          if (formData.ruleColumn && formData.ruleColumn.length > 0) {
            formData.ruleColumn = formData.ruleColumn.trim()
          }
          if (formData.ruleValue && formData.ruleValue.length > 0) {
            formData.ruleValue = formData.ruleValue.trim()
          }
          let obj
          if (!this.model.id) {
            obj = apiAddPermissionRule(formData)
          } else {
            obj = apiEditPermissionRule(formData)
          }
          obj.then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok')
              this.close()
            } else {
              this.$message.warning(res.message)
            }
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    },
    handleCancel () {
      this.close()
    },
    handleChangeRuleCondition (val) {
      if (val === 'USE_SQL_RULES') {
        this.form.setFieldsValue({
          ruleColumn: ''
        })
        this.showRuleColumn = false
      } else {
        this.showRuleColumn = true
      }
    }
  }
}
</script>

<style scoped>

</style>
